package com.jeesite.modules.users.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.edu.ncu.util.UserStatus;
import com.jeesite.modules.banrecord.entity.BanRecord;
import com.jeesite.modules.banrecord.service.BanRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.users.entity.Users;
import com.jeesite.modules.users.dao.UsersDao;

/**
 * usersService
 * @author k
 * @version 2025-08-21
 */
@Service
public class UsersService extends CrudService<UsersDao, Users> {
	@Autowired
	private BanRecordService banRecordService;
	@Autowired
	private UsersDao usersDao;
	/**
	 * 获取单条数据
	 * @param users
	 * @return
	 */
	@Override
	public Users get(Users users) {
		return super.get(users);
	}
	
	/**
	 * 查询分页数据
	 * @param users 查询条件
	 * @param users page 分页对象
	 * @return
	 */
	@Override
	public Page<Users> findPage(Users users) {
		return super.findPage(users);
	}
	
	/**
	 * 查询列表数据
	 * @param users
	 * @return
	 */
	@Override
	public List<Users> findList(Users users) {
		return super.findList(users);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param users
	 */
	@Override
	@Transactional
	public void save(Users users) {
		super.save(users);
	}
	
	/**
	 * 更新状态
	 * @param users
	 */
	@Override
	@Transactional
	public void updateStatus(Users users) {
		super.updateStatus(users);
	}
	
	/**
	 * 删除数据
	 * @param users
	 */
	@Override
	@Transactional
	public void delete(Users users) {
		super.delete(users);
	}
	/**
	 * 封禁用户
	 * @param users
	 */

	@Transactional
	public void ban(Users users, String reason) {
		//需要管理员审核是否真的违规
		//根据封禁原因计算unbanDate
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, 7); // 加7天
		Date unbanDate = calendar.getTime();
		doban(users,unbanDate,reason);
	}
	public void doban(Users users, Date unbanDate, String reason) {
		users.setStatus(UserStatus.BANNED.getName());
		System.out.println(users);
		save(users);
		//清除token
		BanRecord banrecord = new BanRecord();
		banrecord.setPlayerName(users.getNickname());
		banrecord.setUuid(users.getTid());
		banrecord.setReason(reason);
		banrecord.setBanDate(new Date());
		banrecord.setUnBanDate(unbanDate);
		banRecordService.save(banrecord);
	}
	public Users findById(Long id){
		return usersDao.selectById(id);
	}
}